package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.DTO.UserDTO;
import com.example.Result.Result;
import com.example.domain.User;
import com.example.service.UserService;
import com.example.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    private Result<UserDTO> login (@RequestBody User user) {
        String username = user.getUserName();
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUserName,username);
        User selectUser = userService.getOne(userLambdaQueryWrapper);
        if (selectUser != null) {
            if (selectUser.getPassword().equals(password)) {
                //登录成功后，生成jwt令牌
                Map<String, Object> claims = new HashMap<>();
                claims.put("userId", selectUser.getUserId());
                claims.put("name", selectUser.getUserName());
                String token = JwtUtil.createJWT(
                        "sziit",
                        7200000,
                        claims);
                UserDTO userDTO = new UserDTO();
                userDTO.setUserId(Integer.valueOf(selectUser.getUserId()));
                userDTO.setName(selectUser.getUserName());
                userDTO.setToken(token);
                userDTO.setRole(selectUser.getRole());
                return Result.success(userDTO);
            } else {
                return Result.error("用户名或密码错误");
            }
        } else {
            return Result.error("用户名或密码错误");
        }
    }

    @PostMapping("/register")
    public Result<String> add (@RequestBody User user) {
        String password = user.getPassword();
        if (user.getPhone() == null) {
            user.setPhone("");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        userService.save(user);
        return Result.success("注册成功");
    }

    @GetMapping("/{userId}")
    public Result<User> getById(@PathVariable Integer userId) {
        User user = userService.getById(userId);
        return Result.success(user);
    }

    @PutMapping
    public Result<String> update (@RequestBody User user) {
        String password = user.getPassword();
        if (password != null){
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            user.setPassword(password);
        }
        userService.updateById(user);
        return  Result.success("用户修改成功");
    }

    @GetMapping
    public Result<Page> getAll(int page, int pageSize, String name) {
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<User> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.like(StringUtils .isNotEmpty(name),User::getUserName,name);
        employeeLambdaQueryWrapper.orderByDesc(User::getUpdateTime);
//        分页查询
        userService.page(pageInfo, employeeLambdaQueryWrapper);
        return Result.success(pageInfo);
//        return null;
    }

    @DeleteMapping("/{userId}")
    public Result<String> delete (@PathVariable Integer userId) {
        userService.removeById(userId);
        return Result.success("删除成功");
    }
}
